//package com.xx.cloud.pay.service.impl;
//
//
//import cn.hutool.core.util.IdUtil;
//import cn.hutool.extra.qrcode.QrCodeUtil;
//import cn.hutool.json.JSONObject;
//import com.github.binarywang.wxpay.bean.notify.*;
//import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
//import com.github.binarywang.wxpay.bean.request.*;
//import com.github.binarywang.wxpay.bean.result.*;
//import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
//import com.github.binarywang.wxpay.exception.WxPayException;
//import com.github.binarywang.wxpay.service.WxPayService;
//import com.xx.cloud.pay.config.pay.PayConFigure;
//import com.xx.cloud.pay.entity.CreateOrderEntity;
//import com.xx.cloud.pay.result.R;
//import com.xx.cloud.pay.result.UUIDGenerator;
//import com.xx.cloud.pay.service.CarPayService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//
//import static com.xx.cloud.pay.result.ImagesUtils.imageToBase64;
//
//
///**
// * @ClassName PayServiceImpl
// * @Author 小坏
// * @Date 2022/1/25、15:43
// * @Version 1.0
// */
//@Slf4j
//@Service
//public class CarPayServiceImpl implements CarPayService {
//
//    @Autowired
//    private WxPayService wxService;
//
//    @Autowired
//    private PayConFigure payConFigure;
//
//
//    /**
//     * 获取几分钟后的时间
//     *
//     * @param min
//     * @return
//     */
//    public static String getAfterMinDate(Integer min) {
//        Date date = new Date();
//        Date newDate = new Date(date.getTime() + 60000 * min);
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formatDate = dateFormat.format(newDate);
//        return formatDate;
//    }
//
//    @Override
//    public R createOrderPerson(CreateOrderEntity createOrderEntity) {
//        try {
//            WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
//            BeanUtils.copyProperties(createOrderEntity, request);
//            //10 就是0.1  -》100 -》就是1
//            WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
//            WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
////            request.setPayer(payer.setOpenid(createOrderEntity.getOpenid()));
//            amount.setCurrency("CNY");
//            amount.setTotal(1);  //金额
//            request.setAmount(amount); //金额对象
//            request.setDescription("买个女人");
//
//
////            // 获取当前时间
////            ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
////
////            // 创建符合RFC 3339的日期时间格式化器
////            DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
////
////            // 格式化日期时间
////            String formattedDateTime = now.format(formatter);
////
////            // 输出结果
////            System.out.println(formattedDateTime);
////
////            request.setTimeExpire(formattedDateTime);
//            String nextIdStr = IdUtil.getSnowflakeNextIdStr();
//            //TODO 商户订单号
//            request.setOutTradeNo(nextIdStr);
//            System.out.println(request);
//            // TODO 0微信 下单
//
//            //TODO 1、支付方式
//            String orderV3 = wxService.createOrderV3(TradeTypeEnum.NATIVE, request);
//
//
//            //保存到数据库
//            String result = imageToBase64(QrCodeUtil.generate(orderV3, 300, 300));
//            JSONObject data = new JSONObject();
//            data.put("code", result);
//            data.put("orderSn", nextIdStr);
//            return R.ok(data);
//        } catch (Exception e) {
//            System.out.println(e);
//            return R.failed("11111");
//        }
//
//    }
//
//    /**
//     * 异步通知成功。。。。。。: {"deviceInfo":"001","openid":"oZfg_hR51FXfUJIfAk","isSubscribe":"N","tradeType":"JSAPI","bankType":"OTHERS","totalFee":200,
//     * "feeType":"CNY","cashFee":200,"couponList":[],"transactionId":"4200005202202119486082624",
//     * "outTradeNo":"2011120234797149198599153513","attach":"上海市","timeEnd":"20220211120332",
//     * "returnCode":"SUCCESS","resultCode":"SUCCESS","appid":"wx3445d4dd3b2","mchId":"16068198",
//     * "nonceStr":"402887d67ee6f26e017ee6f26eab0000","sign":"370AFCEEFC9F7E4B6C2382F",
//     *
//     * @param xmlData
//     * @return
//     */
//    @Override
//    public String parseOrderNotifyResultPerson(String xmlData) {
//
//        try {
//            final WxPayOrderNotifyResult notifyResult = wxService.parseOrderNotifyResult(xmlData);
//            //TODO 设置交易流水
//            // TODO 根据自己业务场景需要构造返回对象
//            String data = WxPayNotifyResponse.success("成功");
//
//            //检查是否支付成功
//            if ("SUCCESS".equals(notifyResult.getReturnCode()) && "SUCCESS".equals(notifyResult.getResultCode())) {
//                log.info("车证异步通知成功......................", notifyResult);
//                /**
//                 * TODO 1、验价、防止篡改订单的价格
//                 *      获取回调支付的金额
//                 *      获取订单号、根据订单号查询本地订单
//                 *      校验返回的订单金额是否与商户侧的订单金额一致
//                 */
//
//                return data;
//
//            }
//            String fail = WxPayNotifyResponse.fail("失败！");
//            return fail;
//
//        } catch (WxPayException e) {
//            return e.getReturnMsg();
//        }
//    }
//
//    @Override
//    public R queryOrder(String transactionId, String outTradeNo) {
//        try {
//            WxPayOrderQueryResult queryOrder = this.wxService.queryOrder(transactionId, outTradeNo);
//            // && "SUCCESS".equals(queryOrder.getReturnCode()) && "SUCCESS".equals(queryOrder.getResultCode())
//            if ("SUCCESS".equals(queryOrder.getTradeState())) {
//                //1 支付成功
//                return R.ok(queryOrder);
//            } else {
//                return R.failed(queryOrder.getTradeStateDesc());
//            }
//
//        } catch (WxPayException e) {
//            if ("FAIL".equals(e.getResultCode())) {
//                return R.failed(e.getErrCodeDes());
//            }
//            return R.failed(e.getReturnMsg());
//        }
//    }
//
//
//    /**
//     * 调用退款成功：WxPayRefundResult(transactionId=4200001309202202113264769693, outTradeNo=20220211185354074149208950390022,
//     * outRefundNo=20220211185354074149208950390022, refundId=50301700732022021117284815967, refundFee=200, settlementRefundFee=null,
//     * totalFee=200, settlementTotalFee=null, feeType=null, cashFee=200, cashFeeType=null, cashRefundFee=200, couponRefundCount=0,
//     * couponRefundFee=0, promotionDetailString=null, promotionDetails=null, refundCoupons=[])
//     * TODO 一、优化使用多线程和MQ 异步执行
//     *
//     * @param request
//     * @return
//     */
//    @Override
//    public R refund(WxPayRefundRequest request) {
//        //设置总金额
//        request.setTotalFee(1);
//        //设置退款金额
//        request.setRefundFee(1);
//        try {
//            WxPayRefundResult refund = this.wxService.refund(request);
//            //TODO 1、如果退款成功
//            if ("SUCCESS".equals(refund.getReturnCode())) {
//                return R.ok("退款成功");
//            }
//
//        } catch (WxPayException e) {
//            if ("FAIL".equals(e.getResultCode())) {
//                return R.failed(e.getErrCodeDes());
//            }            //失败返回微信方错误
//            return R.failed(e.getErrCodeDes());
//        }
//
//        return R.failed("FAIL");
//    }
//
//    /**
//     * 退款 查询
//     *
//     * @param transactionId
//     * @param outTradeNo
//     * @param outRefundNo
//     * @param refundId
//     * @return
//     */
//    @Override
//    public R refundQuery(String transactionId, String outTradeNo, String outRefundNo, String refundId) {
//        try {
//            WxPayRefundQueryResult wxPayRefundQueryResult = this.wxService.refundQuery(transactionId, outTradeNo, outRefundNo, refundId);
//
//            if ("SUCCESS".equals(wxPayRefundQueryResult.getReturnCode())) {
//                List<WxPayRefundQueryResult.RefundRecord> refundRecords = wxPayRefundQueryResult.getRefundRecords();
//                for (WxPayRefundQueryResult.RefundRecord record : refundRecords) {
//                    String refundStatus = record.getRefundStatus();
//                    //判断退款状态
//                    if ("SUCCESS".equals(refundStatus)) {
//                        //退款成功
//                        return R.ok(wxPayRefundQueryResult, "退款成功");
//                    }
//
//                    return R.failed("退款处理中！");
//                }
//
//            }
//        } catch (WxPayException e) {
//            if ("FAIL".equals(e.getErrCodeDes())) {
//                return R.failed("not exist");
//            }
//            return R.failed(e.getReturnMsg());
//        }
//
//
//        return R.failed("退款失败");
//    }
//
//    @Override
//    public R closeOrder(String transactionId, String outTradeNo) {
//
////        //TODO 1、调用查询订单 queryOrder(String transactionId, String outTradeNo)
//        try {
//
//            //TODO 1、调用查询订单 queryOrder(String transactionId, String outTradeNo)
//
//            //TODO 2、关单
//            WxPayOrderCloseResult wxPayOrderCloseResult = this.wxService.closeOrder(outTradeNo);
//
//            //TODO 3、修改订单表状态
//
//            log.info("修改订单: ");
////            apiOrderService.closeOrderStatus(outTradeNo);
//            return R.ok(wxPayOrderCloseResult);
//        } catch (WxPayException e) {
//            return R.failed(e.getReturnMsg());
//        }
//    }
//
//    /**
//     * 退款通知回调
//     * 退款通知回调
//     * WxPayRefundNotifyResult(
//     * reqInfoString=MJrSMdXd2R2y1kJVAkLmSzfIROXw9myBP9xPemD6J80kFLeJSHjTlKQN0g4XR4PeyS+1YGHgjZ+QDCCZDTKzEs540kPLGt3lTiAgD+YYMh/Am9xo7+2PCQEMnaqlxatQ2GezC0cvPWr3zYRQrhk5qJkfzdoO5bA4KbG0xjX7CeTVk5NIqW9xPIEjRfjbCWsly0In1Ro7kEbSyiRu8B8JFQ2Tjse5r5QGJI3AoPAo5xrBtW4TGSu5hubWkqjVrTjOgnJv22Wx4HvTgkRonIOWehYMGM7POxC4mFUgW2ZgA9DtSjRXBJDFWfhCMauv8zuRJPXSl0qaRN7X7wcK3N9F2cTagaO1Vd75ZSzA+ZDN2Y7ICdyoLkdhBAM2AntIUj80gBlgSH1YVRz4KFeH+Etv21gwgoNRCePOVF0gLawaDuf+dJXLcSKS8lVDQvosis83bjEZOIfbDXn+sd8l5vRhjemDOxlzEVhZR0VqBz3figeXrUP+HJh4bI9w5RzLUOnyKCv18qN4JPmVh0hYq6WiVzm+YEzWyrpoE5mxQZrUvl1x69+2cFJKwCE/d6vPtVpiYkgF+m+AxcWX1egYI1RSagU80y2zcBcQ//eF8HWWxzUYhZTmO6syQUabN6P6irL8SfH5jAJwjcu1rN7QRaLeCU9MlWVy62582Azv5S16JXEqpxtmB0P8tiOpS9SoesUsmPbZHs/PHb6IPlGuVZ2S5FiS1YUarLIF5zws4gSXjmgLf3to+RrIUCRC6tD5lqxnfLx9pP4ETUpwYCeUbwbYmuCQpcM8R7kcCCSEQdvvVIFxNAh379S9OC0FRk548v3uXctfLxe1SaNEirUBUdz6Gy3hni40wXL9RxnniTjpUzDCPxga0P5Pr4yS2g1hOVEtw6yjyjF1m7ai1nFDBfVrIh7utaLu8Bl2HBXah98569vqvSxf3alLm28YI+zEvpn2/CZ6Ex4G4Tc55zjrX6YZf96jUBzSRf5by5rJbalNzakn15tjSIEK3Umskfucz5rtEtpF1i+oFTygwA1zXrNm+ZKFcVrFQVVlaho8RWmTBWnyU8Okv8T5z1BO69vZTd2DbGKJ1pNkFkNUHbm1+ItvUA==,
//     * reqInfo={"transactionId":"4200001309202202113264769693","outTradeNo":"20220211185354074149208950390022",
//     * "refundId":"50301700732022021117284815967","outRefundNo":"20220211185354074149208950390022","totalFee":200,
//     * "settlementTotalFee":200,"refundFee":200,"settlementRefundFee":200,"refundStatus":"SUCCESS","successTime":"2022-02-11 18:58:27",
//     * "refundRecvAccout":"支付用户零钱","refundAccount":"REFUND_SOURCE_RECHARGE_FUNDS","refundRequestSource":"API"}, decryptedReqInfo=null)
//     *
//     * @param xmlData
//     * @return
//     */
//    @Override
//    public WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) {
//        try {
//            WxPayRefundNotifyResult result = this.wxService.parseRefundNotifyResult(xmlData);
//            log.info("退款通知回调: ", result);
//
//        } catch (WxPayException e) {
//            log.info("异常：", e);
//        }
//        return null;
//    }
//
//    @Override
//    public R<?> createOrderPerson1(CreateOrderEntity createOrderEntity) {
//        try {
//            WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
//            BeanUtils.copyProperties(createOrderEntity, request);
//            //10 就是0.1  -》100 -》就是1
//            request.setTotalFee(1); //自定义金额
//            request.setFeeType(payConFigure.getFeeType()); //人民币
//            request.setNonceStr(UUIDGenerator.generate());//随机字符串
//            String orderSn = "111111111111111111111111111111";
//            String orderS = orderSn.substring(0, orderSn.length() - 4);
//            request.setOutTradeNo(orderS); //商户订单号
//
//            // TODO 0微信 下单
//            WxPayMpOrderResult order = wxService.createOrder(request);
//            return R.ok(order);
//        } catch (WxPayException e) {
//            return R.failed(e.getReturnMsg());
//        }
//    }
//
//    @Override
//    public R refundV2(WxPayRefundRequest request) {
//
//
//        return null;
//    }
//
//    @Override
//    public R refundV33(WxPayRefundV3Request request) throws WxPayException {
//        try {
//
//            WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
//            amount.setTotal(request.getAmount().getTotal()); //总金额
//            amount.setRefund(request.getAmount().getRefund()); //退款金额
//            amount.setCurrency("CNY");
//            //设置总金额
//            request.setAmount(amount);
//
//            //设置退款金额
//            request.setOutRefundNo(request.getOutRefundNo());
//            request.setOutTradeNo(request.getOutTradeNo());
//
//            WxPayRefundV3Result wxPayRefundV3Result = this.wxService.refundV3(request);
//            System.out.println(wxPayRefundV3Result);
//            return R.ok(wxPayRefundV3Result);
//        } catch (WxPayException e) {
//            log.debug("退款异常！", e);
//            return R.failed(e.getCustomErrorMsg());
//        }
//    }
//
//    @Override
//    public R<?> closeOrderV33(WxPayUnifiedOrderV3Request request) {
//        //TODO 1、调用查询订单 queryOrder(String transactionId, String outTradeNo)
//        try {
//            //TODO 2、关单
//            Object orderV3 = this.wxService.createOrderV3(TradeTypeEnum.JSAPI, request);
//            log.info("修改订单: " + orderV3);
//            return R.ok();
//        } catch (WxPayException e) {
//            return R.failed(e.getReturnMsg());
//        }
//    }
//
//
//    @Override
//    public R queryOrderV3(WxPayOrderQueryV3Request request) {
//        try {
//            WxPayOrderQueryV3Result v3Result = this.wxService.queryOrderV3(request);
//            if ("SUCCESS".equals(v3Result.getTradeState())) {
//                //1 支付成功
//                return R.ok(v3Result);
//            } else {
//                return R.failed(v3Result.getTradeStateDesc());
//            }
//        } catch (WxPayException e) {
//            log.debug("订单查询", e);
//            return R.failed(e.getCustomErrorMsg());
//        }
//    }
//
//    @Override
//    public R<?> refundQueryV3(WxPayRefundQueryV3Request request) {
//        try {
//            WxPayRefundQueryV3Result v3Result = this.wxService.refundQueryV3(request);
//            return R.ok(v3Result);
//        } catch (WxPayException e) {
//            log.debug("退款数据查询", e);
//            return R.failed(e.getCustomErrorMsg());
//        }
//    }
//
//    @Override
//    public WxPayRefundNotifyResult parseRefundNotifyResultV3(String xmlData) {
//        try {
//            SignatureHeader header = new SignatureHeader();
//            WxPayRefundNotifyV3Result v3Result = this.wxService.parseRefundNotifyV3Result("", header);
//            log.info("退款通知回调: ", v3Result);
//
//        } catch (WxPayException e) {
//            log.info("异常：", e);
//        }
//        return null;
//    }
//
//
//    /**
//     * id=771da284-c075-58fa-97d6-628111de8fc5,
//     * createTime=2023-12-15T17:41:50+08:00,
//     * eventType=TRANSACTION.SUCCESS,
//     * summary=支付成功,
//     * resourceType=encrypt-resource,
//     * resource=OriginNotifyResponse.Resource(
//     * algorithm=AEAD_AES_256_GCM,
//     * originalType=transaction,
//     * ciphertext=PyM8BkFZDD1naeTf4BK27J1wJoOolvCdUBGcR2eJlQqLrOYMD8gO/XtPLUXJUWvwGJE5WzLwhV+26eV5dlO1mQ+3AUfwnEVEnQ5ixE7epwajsmTpPVuFmcP+3pA3hrw0bVuLxzIuFcM1ooHw18eSM5GeUTCxxAtQV0tgjt6x2NSeMYOsbqRZFOxbMLVyJMtpS7K+qlepgsJfWZ7F58g9K4d9/Gr6xrg6R3NYsM6bLm4Yjt69wlAsKnAjskZqnz/HTyacy2vs1zwESNFKFPc0c+ajOZO9tjiZGaVOKgmphG5G4tjyrkcNmqPkwRTe0nulTiYKIMtPfqRnNPffnMjpPf0wTenyDIHuDm3wbvz7LeoU6TD650HbwYpan6MoCmwJjVTb/w61oM+6MJ7AtQhd56kaxyx5q473gGuqiikoR7sLOncU/CTTORbyffbUq9YGBo5qqXJUZ5O1iXypLRVdAsjSOt8Wk+BXSOiZGNfyQlcB/8aQzPtXmF7ghZFCJMlUeujQTEJRPL3FlzicgcgAEgSiEwgva1p9iDLNXnTdtXvjq2qeHst8AH5ZzTEw9/o/H1nY,
//     * associatedData=transaction,
//     * nonce=5QrkJ1vbEWDH
//     * )), result=WxPayOrderNotifyV3Result.DecryptNotifyResult(
//     * appid=wxf001758b92227b,
//     * mchid=1607951,
//     * outTradeNo=1735595943883284480,
//     * transactionId=4200002118202312157381245633,
//     * tradeType=NATIVE,
//     * tradeState=SUCCESS,
//     * tradeStateDesc=支付成功,
//     * bankType=OTHERS, attach=,
//     * successTime=2023-12-15T17:41:50+08:00,
//     * payer=WxPayOrderNotifyV3Result.Payer(openid=oaXAQvz-6RA093v78QLu89v2Aayg),
//     * amount=WxPayOrderNotifyV3Result.
//     * Amount(total=1,
//     * payerTotal=1,
//     * currency=CNY,
//     * payerCurrency=CNY),
//     * sceneInfo=null,
//     * promotionDetails=null))
//     *
//     * @param notifyData
//     * @param headers
//     * @return
//     */
//    @Override
//    public String parseOrderNotifyResultPersonV3(String notifyData, SignatureHeader headers) {
//        try {
//            WxPayNotifyV3Result v3Result = wxService.parseOrderNotifyV3Result(notifyData, headers);
//
//            //TODO 设置交易流水
//            String data = WxPayNotifyResponse.success("成功");
//
//            WxPayNotifyV3Result.DecryptNotifyResult result = v3Result.getResult();
//
//            //获取单号 根据单号查找数据库是否支付完成
//            String outTradeNo = result.getOutTradeNo();
//
//            //数据库查询
//            //String data =            select.get(id)
//
//
////            data.getstate
////
//            if ("已支付".equals("已支付")) {
//                System.out.println("已支付: " + v3Result);
//                //1、修改状态
//                //检查是否支付成功
//                return data;
//            }
//
//            //检查是否支付成功
//            return data;
//        } catch (WxPayException e) {
//            return e.getReturnMsg();
//        }
//    }
//
//
//}
//
